package database.paydetail;

import database.CRUD;

import javax.swing.table.DefaultTableModel;
import java.sql.*;

public class tb_pay_detail {//采购明细表
    private int PDid;//采购明细ID
    private int Pid;//采购清单
    private String Gid;//商品编号
    private int Pcount;//采购数量
    private int Gpay;//商品单价
    private float total;//商品总价
    private String other;//备注
    public tb_pay_detail() {}
    public tb_pay_detail(int PDid, int Pid, String Gid, int Pcount, int Gpay, float total, String other){
        this.PDid = PDid;
        this.Pid = Pid;
        this.Gid = Gid;
        this.Pcount = Pcount;
        this.Gpay = Gpay;
        this.total = total;
        this.other = other;
    }

    public void setPDid(int PDid) {
        this.PDid = PDid;
    }
    public void setPid(int Pid) {
        this.Pid = Pid;
    }
    public void setGid(String Gid) {
        this.Gid = Gid;
    }
    public void setPcount(int Pcount) {
        this.Pcount = Pcount;
    }
    public void setGpay(int Gpay) {
        this.Gpay = Gpay;
    }
    public void setTotal(float total) {
        this.total = total;
    }
    public void setOther(String other) {
        this.other = other;
    }
    public int getPDid() {
        return PDid;
    }
    public int getPid() {
        return Pid;
    }
    public String getGid() {
        return Gid;
    }
    public int getPcount() {
        return Pcount;
    }
    public int getGpay() {
        return Gpay;
    }
    public float getTotal() {
        return total;
    }
    public String getOther() {
        return other;
    }
    public void settb_pay_detail(tb_pay_detail tb_pay_detail){
        this.total = tb_pay_detail.getTotal();

    }
    public float counttotal(int Pcount,int Gpay){
        float total=Pcount*Gpay;
        return total;
    }
    // 插入新采购明细
    public boolean insertPayDetail(int PDid, int Pid, String Gid, int Pcount, float Gpay, float total, String other) {
        String sql = "INSERT INTO tb_pay_detail (PDid, Pid, Gid, Pcount, Gpay, total, other) "
                + "VALUES (?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC", "root", "123456");
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            // 设置预编译语句的参数值
            pstmt.setInt(1, PDid);
            pstmt.setInt(2, Pid);
            pstmt.setString(3, Gid);
            pstmt.setInt(4, Pcount);
            pstmt.setFloat(5, Gpay);
            pstmt.setFloat(6, total);
            pstmt.setString(7, other);

            // 执行更新操作
            int rowsAffected = pstmt.executeUpdate();

            if (rowsAffected > 0) {
                return true; // 插入成功
            } else {
                return false; // 插入失败
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 插入失败
        }
    }


    // 更新采购明细信息
    public boolean updatePayDetail(int PDid, int Pid, String Gid, int Pcount, int Gpay, String other) {
        String sql = "UPDATE tb_pay_detail SET Pid = ?, Gid = ?, Pcount = ?, Gpay = ?, total = ?, other = ? "
                + "WHERE PDid = ?";
        Object[] params = {Pid, Gid, Pcount, Gpay, total, other, PDid};
        try {
            float total = Pcount * Gpay; // 计算总价
            CRUD.executeUpdate(sql);
            return true; // 更新成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 更新失败
        }
    }

    // 删除采购明细记录
    public boolean deletePayDetail(String attribute, String value) {
        String sql;
        if ("PDid".equalsIgnoreCase(attribute)) {
            //如果是PDid的的话，直接查询
            sql = "DELETE FROM tb_pay_detail WHERE PDid = ?";
        } else {
            // 如果是其他的需要添加属性查询
            sql = "SELECT * FROM tb_pay_detail WHERE " + attribute + " = ?";
        }

        try {
            PreparedStatement pstmt = CRUD.getConnection().prepareStatement(sql);
            pstmt.setString(1, value);

            if ("PDid".equalsIgnoreCase(attribute)) {
                int rowsAffected = pstmt.executeUpdate();
                return rowsAffected > 0;
            } else {
                //sql查询出来的行
                ResultSet rs = pstmt.executeQuery();
                int rowCount = 0;
                while (rs.next()) {
                    rowCount++;
                    int PDid = Integer.parseInt(rs.getString("PDid"));
                    int pid = Integer.parseInt(rs.getString("Pid"));
                    System.out.println("找到采购明细：PDid=" + PDid + ", Pid" + pid);
                }

                if (rowCount == 0) {
                    System.out.println("没有找到 " + attribute + " = " + value + " 的采购明细");
                    return false;
                } else if (rowCount == 1) {

                    int rowsAffected = pstmt.executeUpdate();
                    return rowsAffected > 0;
                } else {
                    System.out.println("找到多个符合条件的采购明细 " + attribute + " = " + value);
                    return false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // Delete failed
        }
    }

    // 查询采购明细信息
    public DefaultTableModel queryPayDetails() {
        String sql = "SELECT * FROM tb_pay_detail";
        try {
            return CRUD.executeQueryAndBuildTableModel(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return null; // 查询失败
        }
    }
}
